package com.zhuiyun.pm.sys.role.service.impl;



import com.zhuiyun.pm.page.MyPageHelper;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.page.PageResult;
import com.zhuiyun.pm.result.CommonResult;
import com.zhuiyun.pm.sys.menu.entity.MenuEntity;
import com.zhuiyun.pm.sys.menu.mapper.MenuMapper;
import com.zhuiyun.pm.sys.role.entity.SysRoleEntity;
import com.zhuiyun.pm.sys.role.mapper.SysRoleMapper;
import com.zhuiyun.pm.sys.role.service.SysRoleService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {


    @Resource
    SysRoleMapper sysRoleMapper;

    @Resource
    MenuMapper menuMapper;

    /**
     * 角色权限菜单树查询
     * @return
     */
    @Override
    public List<MenuEntity> getMenuTree(String menuName) {
        Example example = new Example(MenuEntity.class);
        example.createCriteria().andEqualTo("menuDelFlag",0);
        List<MenuEntity> menuEntityList = menuMapper.selectByExample(example);
        List<MenuEntity> newMenuTree = new ArrayList<>();
        for(MenuEntity menuEntity :menuEntityList){
            if(menuEntity.getMenuParentId()==0){
                newMenuTree.add(getChild(menuEntity,menuEntityList));
            }
        }
        return newMenuTree;
    }

    /**
     * 角色权限菜单树查询
     * @param menuEntity
     * @param menuEntityList
     * @return
     */
    private MenuEntity getChild(MenuEntity menuEntity, List<MenuEntity> menuEntityList) {
        List<MenuEntity> newMenuTree = new ArrayList<>();
        for(MenuEntity childType: menuEntityList){
            if(menuEntity.getId()==childType.getMenuParentId()){
                newMenuTree.add(getChild(childType,menuEntityList));
            }
        }
        menuEntity.setChilds(newMenuTree);
        return menuEntity;
    }

    /**
     * 全查+模糊查
     * @param pageRequest
     * @param roleName
     * @return
     */
    @Override
    public PageResult roleList(PageRequest pageRequest, String roleName) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(SysRoleEntity.class);
        example.createCriteria().andEqualTo("roleDelFlag", 0).andLike("roleName","%"+roleName+"%");
        PageResult pageResult = MyPageHelper.pageInfo(sysRoleMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * 新增角色
     * @param sysRoleEntity
     * @return
     */
    @Override
    public Integer saveRole(SysRoleEntity sysRoleEntity) {
        sysRoleEntity.setRoleDelFlag(0);
        sysRoleEntity.setRoleCreateTime(new Date());
        return sysRoleMapper.insertUseGeneratedKeys(sysRoleEntity);
    }

    /**
     * 修改角色
     * @param sysRoleEntity
     * @return
     */
    @Override
    public Integer updateRole(SysRoleEntity sysRoleEntity) {
        sysRoleEntity.setRoleUpdateTime(new Date());
        return sysRoleMapper.updateByPrimaryKeySelective(sysRoleEntity);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public Integer deleteRole(Integer id) {
        SysRoleEntity roleEntity = new SysRoleEntity();
        roleEntity.setRoleDelFlag(1);
        roleEntity.setId(id);
        return sysRoleMapper.updateByPrimaryKeySelective(roleEntity);
    }

    /**
     * 权限修改
     * @param sysRoleEntity
     * @return
     */
    @Override
    public Integer savePermissions(SysRoleEntity sysRoleEntity) {
        return sysRoleMapper.updateByPrimaryKeySelective(sysRoleEntity);
    }

    /**
    * @Description 获取角色列表信息
    * @Author lzy
    * @Date 2023/3/14 19:48
    * @Return com.zhuiyun.pm.result.CommonResult
    */
    @Override
    public CommonResult getRoleList() {
        List<SysRoleEntity> sysRoleEntities = sysRoleMapper.selectAll();
        return CommonResult.ok(sysRoleEntities);
    }

}
